﻿

using ExcelDataReader;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IXXAT
{
    public class Pubclass
    {
        public struct MonitorInfo
        {
            public int pName;   //参数名称
            public int pAddr;   //参数地址
            public int pStartBit;//起始位置
            public int pBitLength;//位长度
            public int pDataType;//数据类型
            public int pUnit;//单位
            public int pGain;//倍数
            public int p0ffset;//偏移量
            public int pMax;//最大值
            public int pMin;//最小值
            public int pBase;    //除数
            public int Step;//步进
            public int pDecimal;//显示小数位
            public ArrayList[] ArrayMonitor;  //数值列表，状态，监视器
            public MonitorInfo(int i)
            {
                pName = 1;
                pAddr = 2;
                pStartBit = 3;
                pBitLength = 4;
                pDataType = 5;
                pUnit = 6;
                pGain = 7;
                p0ffset = 8;
                pMax = 9;
                pMin = 10;
                pBase = 11;
                Step = 12;
                pDecimal = 13;
                ArrayMonitor = new ArrayList[1024];
                for (Int16 j = 0; j < 1024; j++)
                {
                    ArrayMonitor[j] = new ArrayList();
                }
            }
        }
        public struct MonitorData
        {
            public Int16[] Monitor;
            public int mNumber;   //参数数量
            public int mPointer;  //参数指针
            public int mAddrlen; //参数地址长度

            public MonitorData(int i)
            {
                Monitor = new Int16[1024];
                mNumber = 0;     //参数数量
                mPointer = 0;    //参数指针
                mAddrlen = 0;    //参数地址长度
            }
        }
        public class PublicCache
        {
            public static MonitorInfo GetInfo = new MonitorInfo(0);
            public static MonitorData GetData = new MonitorData(0);
            /// <summary>
            /// 读取参数
            /// </summary>
            /// <param name="pointer">某个参数位置</param>
            /// <returns></returns>
            public int DataReadFormBuffer(int pointer)
            {
                int tmpAddress = int.Parse(GetInfo.ArrayMonitor[pointer][GetInfo.pAddr].ToString());
                int tmpData = GetData.Monitor[tmpAddress];
                int tmpStartBit = int.Parse(GetInfo.ArrayMonitor[pointer][GetInfo.pStartBit].ToString());
                int tmpBitLength = int.Parse(GetInfo.ArrayMonitor[pointer][GetInfo.pBitLength].ToString());
                if (tmpStartBit != 0 || tmpBitLength != 16)
                {
                    tmpData >>= tmpStartBit;//右移低位
                    int tmpBit = 1;
                    for (int i = 0; i < tmpBitLength; i++)
                    {
                        tmpBit *= 2;
                    }
                    tmpBit -= 1;
                    tmpData &= tmpBit;//取余
                }
                return tmpData;
            }
            public static string RetainDecimal(float Data, int Precision)
            {
                switch (Precision)
                {
                    case 1:
                        return $"{Data:F1}";

                    case 2:
                        return $"{Data:F2}";

                    case 3:
                        return $"{Data:F3}";

                    case 4:
                        return $"{Data:F4}";

                    default:
                        return $"{Data:F0}";
                }
            }

           
            public static bool ReadTableMonitor(string path)
            {
                try
                {

                    FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                    StreamReader streamReader = new StreamReader(fileStream, Encoding.GetEncoding("gb2312"));
                    string l_line;
                    string[] l_lineSplit = new string[25];
                    //
                    l_line = streamReader.ReadLine();
                    // int cpslen = 0;
                    //GetData.mNumber = 0;
                    // GetData.mAddrlen = 0;
                    //
                    for (int i = 0; i < 1024; i++)
                    {
                        if ((l_line = streamReader.ReadLine()) != null)
                        {
                            l_lineSplit = l_line.Split(',');
                            GetData.mNumber++;
                            GetInfo.ArrayMonitor[i].AddRange(l_lineSplit);
                            // cpslen = Convert.ToString(GetInfo.ArrayMonitor[i][GetInfo.PAddr]);
                            //if (cpslen > GetData.mAddrlen)
                            //{
                            //    GetData.mAddrlen = cpslen;
                            //}
                        }
                        else

                            break;


                        }
                        GetData.mAddrlen++;
                        streamReader.Close();
                        streamReader.Dispose();
                        fileStream.Close();
                        fileStream.Dispose();
                        return true;
                    
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

    }
   
}
